package com.example.util;

import java.io.*;
import java.lang.reflect.Field;
import java.util.Properties;

public class ConfigProperties {
    private String propertyFileName;
    private Properties properties = new Properties();

    public ConfigProperties() {
    }

    public ConfigProperties(String fileName) {
        this.propertyFileName = fileName;
    }

    protected void loadFromFile(){
        InputStream in = null;
        InputStreamReader ireader = null;
        try {
            ClassLoader cl = ConfigProperties.class.getClassLoader();
            in = cl.getResourceAsStream(propertyFileName);
            if (null != in) {
                ireader = new InputStreamReader(in, "UTF-8");
            }else{
                String filePath = IOUtil.getResourcePath(propertyFileName);
                in = new FileInputStream(filePath);
                ireader = new InputStreamReader(in, "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按key获取值
     */
    public String readProperty(String key){
        String value = "";
        value = properties.getProperty(key);
        return value;
    }

    public String getValue(String key){
        return readProperty(key);
    }

    public int getIntValue(String key){
        return Integer.parseInt(readProperty(key));
    }

    public static ConfigProperties loadFromFile(Class aClass) throws IllegalAccessException{
        ConfigProperties propertiesUtil = null;
        return propertiesUtil;
    }

    public static void loadAnnotations(Class aClass){
        ConfigProperties configProperties = null;
        try {
            configProperties = loadFromFile(aClass);
            if(null == configProperties) return;

            Field[] fields = aClass.getDeclaredFields();

        }catch (IllegalAccessException e){
            e.printStackTrace();
        }
    }
}
